<?php
namespace app\common\model;

use think\Model;

class AuthGroupAccess extends Model
{
    /**
     * @param $id
     * @param array $req
     * @return bool
     */
    public function saveDate($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $data = $req;
        $res = $this->save($data, ['id' => $id]);
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * @param array $data
     * @return bool|mixed
     */
    public function addData(array $data)
    {
        $this->data = [];
        $res = $this->allowField(true)->data($data,true)->isUpdate(false)->save();
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * 删除指定条件的记录
     *
     * @param array $params
     * @return false | integer
     */
    public function delData(array $params)
    {
        if (empty($params)) {
            return false;
        }

        $where = [];

        if (isset($params['uid']) && !empty($params['uid'])) {
            if (is_array($params['uid'])) {
                $where['uid'] = ['in', $params['uid']];
            } else {
                $where['uid'] = $params['uid'];
            }
        }

        if (isset($params['group_id']) && !empty($params['group_id'])) {
            if (is_array($params['group_id'])) {
                $where['group_id'] = ['in', $params['group_id']];
            } else {
                $where['group_id'] = $params['group_id'];
            }
        }

        if (empty($where)) {
            return false;
        }

        $res = $this->where($where)->delete();
        if ($res === false) {
            \think\Log::record('删除数据失败:' . $this->getLastSql());
        }
        return $res;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['uid']) && !empty($params['uid'])) {
            if (is_array($params['uid'])) {
                $where['uid'] = ['in', $params['uid']];
            } else {
                $where['uid'] = $params['uid'];
            }
        }

        if (isset($params['group_id']) && !empty($params['group_id'])) {
            if (is_array($params['group_id'])) {
                $where['group_id'] = ['in', $params['group_id']];
            } else {
                $where['group_id'] = $params['group_id'];
            }
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->paginate($limit);

        return $data->toArray();
    }

    /**
     * 获取指定用户的全部角色id数组
     * @param $user_id
     * @return array
     */
    public function getUserAllGroupIds($user_id)
    {
        if ($user_id <= 0) {
            return [];
        }

        $list = $this->where(['uid' => $user_id])->paginate(100);
        $list = $list->toArray();
        return array_column($list['data'], 'group_id');
    }
}